<?php
/**
 * @author by bilee.
 * 时间: 17/2/15 下午2:52
 * 跑客数据模型表
 */
namespace Common\Model;

use Common\Model\CommonModel;

class DispatchModel extends CommonModel
{
    /**
     * 根据跑客ID获取跑客信息数据
     * @param int $dispatch_id 跑客ID
     * @return mixed
     */
    function get_dispatch_info($dispatch_id){
         return $this->where(array('dispatch_id'=>$dispatch_id))->find();
    }

    /**
     * 跑客端首页数据
     * v1(pnOrderAmount)
     * cash_balance:账户余额，todaytime:今日在线时间，todayordernum:今日接单数，今日收入，totalmileage:订单总里程，avatar:头像，dispatch_name:姓名，job_number:工号，praise:星级(好评率)，company_id:所属团队，未读消息数量，接单热点
     * @param int $dispatch_id 跑客ID
     * @return mixed
     */
    public function pk_index_data($dispatch_id){

        //声明最终的数据
        $ret_data = array();
        $order = D('Order');
        $dispatch_balance = D('DispatchBalance');
        $company = D('Company');
        $order_resmsm = D('OrderResmsm');
        $dispatch_level= D('DispatchLevel');

        //跑客信息组
        $ret_data['dispatch_info'] = $this->where(array('dispatch_id'=>$dispatch_id))->field('mobile,company_id,cash_balance,todaytime,avatar,dispatch_name,totalmileage,job_number,praise,is_audit,is_accept_dispatch,dispatch_level_id')->find();
        if ($ret_data['dispatch_info']['job_number'] === null) {
            $ret_data['dispatch_info']['job_number'] = "暂无工号";
        }
        if ($ret_data['dispatch_info']['dispatch_name'] === null) {
            $ret_data['dispatch_info']['dispatch_name'] = $ret_data['dispatch_info']['mobile'];
        }
        //今日订单数
        $ret_data['dispatch_data']['todayordernum'] = $order->get_dispatch_today_order_num($dispatch_id);
        //今日收入
        $ret_data['dispatch_data']['todayordermoney'] = $dispatch_balance->get_today_money($dispatch_id);
        //所属团队
        $ret_data['dispatch_data']['company_name'] = $company->get_company_name($ret_data['dispatch_info']['company_id']);
        //未读消息个数
        $ret_status = login_upmsg_sys(I('uid'), 2);//在登录或由后台转前台时，将新的公告信息，写入message_sys表中
        //冗余两个字段
        $ret_data['dispatch_data']['cash_balance'] = $ret_data['dispatch_info']['cash_balance'];
        $ret_data['dispatch_data']['todaytime'] = $ret_data['dispatch_info']['todaytime'];
        $ret_data['dispatch_data']['is_accept_dispatch'] = $ret_data['dispatch_info']['is_accept_dispatch'];
        $msg_count = 0;
        if ($ret_status) {
            $msg_count = getmsg_sys_unread($dispatch_id, 2);
        }
        $ret_data['dispatch_data']['msg_count'] = $msg_count;
        $ret_data['serv_charge_rate'] = $dispatch_level->get_serv_charge_rate($ret_data['dispatch_info']['dispatch_level_id']);

        //已分配未取货的订单信息
        $ret_data['order_data']=$order_resmsm->pk_order_detail();
        return $ret_data;
    }

}
